package saleMarket.service;

import saleMarket.dao.FreightCargoDao;
import saleMarket.domain.FreightCargo;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class FreightCargoService {
    private FreightCargoDao freightCargoDao = FreightCargoDao.getInstance();
    private static FreightCargoService freightCargoService = new FreightCargoService();

    //Singleton
    private FreightCargoService() {
    }

    public static FreightCargoService getInstance() {
        return freightCargoService;
    }

    public Collection<FreightCargo> findAll()
            throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<FreightCargo> freightCargos =
                freightCargoDao.findAll(connection);
        JdbcHelper.close(connection);
        return freightCargos;
    }

    public FreightCargo find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        FreightCargo freightCargo = freightCargoDao.find(id, connection);
        JdbcHelper.close(connection);
        return freightCargo;
    }

    public boolean update(FreightCargo freightCargo) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean updated = false;
        try {
            //增加用户
            updated = freightCargoDao.update(freightCargo, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public Integer add(FreightCargo freightCargo) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Integer id =null;
        try {
            //增加用户
            id = freightCargoDao.add(freightCargo, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return id;
    }

    public boolean delete(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        boolean deleted = false;
        try {
            //增加用户
            deleted = freightCargoDao.delete(id, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            //回滚
            connection.rollback();

        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }
}
